﻿using Cinch;
using FR.SAFTAnalyser.ViewModels.Pane;
using MEFedMVVM.Common;
using MEFedMVVM.ViewModelLocator;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FR.SAFTAnalyser.ViewModels.Tools
{
    public class DocumentPartsViewModel : ToolViewModel, ITool
    {
        public DocumentPartsViewModel()
            : base("Tabelas")
        {
            if (!Designer.IsInDesignMode)
            {
                DoOpenSaftPartCommand = new SimpleCommand<object, object>(CanOpenSaftPart, OpenSaftPart);
                DoOpenStockPartCommand = new SimpleCommand<object, object>(CanOpenStockPart, OpenStockPart);
            }
        }

        DocumentPartDetails currentSaftPart;
        public DocumentPartDetails CurrentSaftPart
        {
            get { return currentSaftPart; }
            set
            {
                currentSaftPart = value;
                NotifyPropertyChanged(Cinch.ObservableHelper.CreateArgs<DocumentPartsViewModel>(x => x.CurrentSaftPart));
            }
        }

        ObservableCollection<DocumentPartDetails> saftParts;
        public ObservableCollection<DocumentPartDetails> SaftParts
        {
            get
            {
                if (saftParts == null)
                {
                    saftParts = new ObservableCollection<DocumentPartDetails>();

                    saftParts.Add(new DocumentPartDetails { Name = DocumentPart.Invoices, Description = "Documentos comerciais" });
                    saftParts.Add(new DocumentPartDetails { Name = DocumentPart.MovementOfGoods, Description = "Mercadorias" });
                    saftParts.Add(new DocumentPartDetails { Name = DocumentPart.Payments, Description = "Recibos" });
                    saftParts.Add(new DocumentPartDetails { Name = DocumentPart.WorkingDocuments, Description = "Documentos conferência" });
                    saftParts.Add(new DocumentPartDetails { Name = DocumentPart.GeneralLedgerEntries, Description = "Movimentos contabilísticos" });
                    saftParts.Add(new DocumentPartDetails { Name = DocumentPart.GeneralLedger, Description = "Planos contas" });
                    saftParts.Add(new DocumentPartDetails { Name = DocumentPart.Customers, Description = "Clientes" });
                    saftParts.Add(new DocumentPartDetails { Name = DocumentPart.Supplier, Description = "Fornecedores" });
                    saftParts.Add(new DocumentPartDetails { Name = DocumentPart.Products, Description = "Produtos" });
                    saftParts.Add(new DocumentPartDetails { Name = DocumentPart.Tax, Description = "Impostos" });
                }

                return saftParts;
            }
            set
            {
                saftParts = value;
                NotifyPropertyChanged(Cinch.ObservableHelper.CreateArgs<DocumentPartsViewModel>(x => x.SaftParts));
            }
        }

        DocumentPartDetails currentStockPart;
        public DocumentPartDetails CurrentStockPart
        {
            get { return currentStockPart; }
            set
            {
                currentStockPart = value;
                NotifyPropertyChanged(Cinch.ObservableHelper.CreateArgs<DocumentPartsViewModel>(x => x.CurrentStockPart));
            }
        }

        ObservableCollection<DocumentPartDetails> stockParts;
        public ObservableCollection<DocumentPartDetails> StockParts
        {
            get
            {
                if (stockParts == null)
                {
                    stockParts = new ObservableCollection<DocumentPartDetails>();

                    stockParts.Add(new DocumentPartDetails { Name = DocumentPart.StockHeader, Description = "Existências" });
                    stockParts.Add(new DocumentPartDetails { Name = DocumentPart.StockProducts, Description = "Stocks" });
                }

                return stockParts;
            }
            set
            {
                stockParts = value;
                NotifyPropertyChanged(Cinch.ObservableHelper.CreateArgs<DocumentPartsViewModel>(x => x.StockParts));
            }
        }

        PaneLocation preferredLocation = new PaneLocation { Location = PaneLocation.LEFT };
        public PaneLocation PreferredLocation
        {
            get { return preferredLocation; }
            set { preferredLocation = value; }
        }

        bool isAutoHidden = true;
        public bool IsAutoHidden
        {
            get { return isAutoHidden; }
            set { isAutoHidden = value; }
        }

        public SimpleCommand<object, object> DoOpenSaftPartCommand { get; set; }
        private void OpenSaftPart(object obj)
        {
            EventToCommandArgs commandArgs = obj as EventToCommandArgs;
            if (commandArgs != null && commandArgs.CommandParameter != null)
                Workspace.Workspace.Instance.ShowDocumentPart(((DocumentPartDetails)commandArgs.CommandParameter).Name);
        }
        private bool CanOpenSaftPart(object arg)
        {
            return SAFTAnalyser.SAFT.SaftAnalyser.Instance.SaftFile != null;
        }

        public SimpleCommand<object, object> DoOpenStockPartCommand { get; set; }
        private void OpenStockPart(object obj)
        {
            EventToCommandArgs commandArgs = obj as EventToCommandArgs;
            if (commandArgs != null && commandArgs.CommandParameter != null)
                Workspace.Workspace.Instance.ShowDocumentPart(((DocumentPartDetails)commandArgs.CommandParameter).Name);
        }
        private bool CanOpenStockPart(object arg)
        {
            return SAFTAnalyser.SAFT.StockAnalyser.Instance.StockFile != null;
        }
    }

    public struct DocumentPartDetails
    {
        public DocumentPart Name { get; set; }
        public string Description { get; set; }
        public string ImageSource { get; set; }
    }

    public enum DocumentPart
    {
        Invoices,
        MovementOfGoods,
        Payments,
        WorkingDocuments,
        InvoicesSummary,
        GeneralLedgerEntries,
        GeneralLedger,
        Customers,
        Supplier,
        Products,
        Tax,
        ConvertPemKey,
        StockHeader,
        StockProducts
    }
}
